<template>
    <view class="head">
        <view class="more">
            <view class="search" v-if="!showInput" @click="showSearchInput">
                <img src="/static/icons/search.png" alt="" class="searchIcon">
            </view>
            <view v-else class="search-container">
                <input type="text" class="search-input" focus="true" @confirm="handleInput" />
                <view @click="hideSearchInput" class="cancel-button">取消</view>
            </view>
            <view class="manage" @click="toggleDeleteMode">{{ deleteMode ? '取消' : '管理' }}</view>
        </view>
    </view>
	<checkbox-group @change="onCheckboxChange">
		<view v-for="(item,index) in group" :key="index"  class="checkbox">
			<checkbox v-if="deleteMode" :value="item.id" />
			<groupCard @goGroupDetail="goGroupDetail(item.id)">
				<template v-slot:headImg>
					<img :src="item.avatar" alt="" class="headImg">
				</template>
				<template v-slot:groupName>
					{{ item.name }}
				</template>
				<template v-slot:groupTag>
					{{ item.acgnName }}
				</template>
			</groupCard>
		</view>
	</checkbox-group>
    <button v-if="deleteMode" @click="handleSureDel" class="sureDel">取消收藏</button>
</template>

<script>
import groupCard from "../../components/groupCard/groupCard.vue"
import {getUserFavorGroupPage,removeFavor} from '@/api/favorite.js'

export default {
    components: {
        groupCard
    },
    data() {
        return {
            value: null,
            showInput: false,
            group: [],
			lastPageNo: null,
			pageSize: 3,
			isLast: false,
			deleteMode: false,
			checkedValue: []
        }
    },
	// 下拉刷新方法
	async onPullDownRefresh() {
		// 重新获取数据
		this.isLast = false
		console.log('下拉刷新')
		this.lastPageNo = null
		this.group = []
		await this.getFavorPageData()
		uni.stopPullDownRefresh()
	},
	async onReachBottom() {
		this.getFavorPageData()
		console.log("onReachBottom");
	},
	onShow() {
		this.getFavorPageData()
	},
	
    methods: {
		onCheckboxChange(value) {
		  this.checkedValue = value.detail.value
		  console.log(this.checkedValue, 'checkedValue', value)
		},
		handleSureDel() {
			this.deleteMode = false
			console.log(this.checkedValue.map(item => parseInt(item)))
			const data = {
				groupIdList: this.checkedValue.map(item => parseInt(item)),
			}
			removeFavor(data).then(res => {
				if(res.data.code==200){
					this.isLast = false
					console.log('下拉刷新')
					this.lastPageNo = null
					this.group = []
					this.getFavorPageData()
				}

			})
			
		},
		toggleDeleteMode() {
		  this.deleteMode = !this.deleteMode; // 切换删除模式
		  this.checkedValue = [];
		},
		deleteGroup(index) {
		  this.group.splice(index, 1); // 从group数组中移除指定项
		},
		getFavorPageData() {
			if (this.isLast) return
			const data = {
				pageNo: "",
				pageSize: this.pageSize,
				key:''
			}
			getUserFavorGroupPage(data).then(res => {
				this.group = [...res.data.data.records]
				this.lastPageNo = res.data.data.pageNo
				this.isLast = res.data.data.isLast
			})
		},
        showSearchInput() {
            this.showInput = true;
        },
        hideSearchInput() {
            this.showInput = false;
        },
        handleInput(e) {
            // 处理搜索框输入内容的逻辑
            const searchText = e.detail.value;
            // 在这里可以根据输入的内容进行搜索操作
			const data = {
				pageNo: "",
				pageSize: this.pageSize,
				key:searchText
			}
			getUserFavorGroupPage(data).then(res => {
				console.log(res.data.data)
				this.group = [...res.data.data.records]
				this.lastPageNo = res.data.data.pageNo
				this.isLast = res.data.data.isLast
			})
        },
		goGroupDetail(id){
			console.log(id)
			uni.navigateTo({
				url:`/pages/groupDetails/groupDetails?groupId=${id}`
			})
		},
    }
}
</script>

<style>
.groupWrap {
	display: flex;
	justify-content: center;
	align-items: center;
}

	
.head {
    display: flex;
    justify-content: space-between;
    width: 100%;
    height: 80rpx;
    line-height: 80rpx;
    font-size: 30rpx;
    color: #4D4D4D;
    background-color: #F3F9FC;
}

/* 右侧搜索和管理 */
.more {
    width: 80%;
    position: relative;
    text-align: center;
}

.more .search {
    position: absolute;
    /* width: 30%;
    height: 100%; */
    top: 15rpx;
	right: 0rpx;
    width: 50rpx;
    height: 50rpx;
    
}

.searchIcon {
    /* position: absolute; */
    /* top: 15rpx;
    right: 0; */
    width: 100%;
    height: 100%;
}

.more .manage {
    position: absolute;
    width: 15%;
    height: 100%;
    right: -20%;
    /* text-align: center; */
    font-size: 30rpx;
    /* color: #4D4D4D; */
}

/* 搜索 */
.search-container {
    position: absolute;
    right:40rpx;
    width: 80%;
    height: 50rpx;
    line-height: 50rpx;
    border: 1px solid #C0E0EE;
    border-radius: 25rpx;
    margin: 15rpx 0;
    display: flex;
    align-items: center;
}

.search-input {
    width: 80%;
    height: 100%;
    text-align: left;
    padding-left: 10rpx;
}

.cancel-button {
    width: 20%;
    height: 100%;
    padding-right: 5rpx;
    color: #84C3DD;
    font-size: 24rpx;
    /* margin-left: 10rpx; */
    /* 在这里设置取消按钮的样式 */
}
.sureDel{
	position: fixed;
	bottom: 30rpx;
	left: 50%;
	transform: translate(-50%,0);
	width: 90%;
	height: 76rpx;
	line-height: 76rpx;
	border-radius: 38rpx;
	font-size: 32rpx;
	background-color: #84C3DD;
}


/* 团信息 */
.backgroundImg {
    width: 100%;
    height: 100%;
}
.info .headImg {
    position: absolute;
    width: 105rpx;
    height: 105rpx;
    border-radius: 50%;
    left: 40rpx;
    top: 0;
    background-color: blue;
}
.checkbox {
	display: flex;
	justify-content: center;
	align-items: center;
}
</style>